 
<%@page import="study.itwill.team5.board.BoardCategoryItem"%>
<%@page import="study.itwill.team5.board.BoardCategory"%>
<%@page import="study.itwill.team5.board.BoardItem"%>
<%@page import="study.itwill.team5.board.Board"%>
<%@page import="study.itwill.common.PatternChecker"%>
<%@page import="study.itwill.team5.member.MemberItem"%>
<%@page import="study.itwill.team5.member.Member"%>
<%@page import="java.util.ArrayList"%> 
<%@page import="study.itwill.common.DBHandler"%>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ page trimDirectiveWhitespaces="true" %>
<%@include file="../dbcon.jsp"%>
<%
	String seq = request.getParameter("seq");

	if (!PatternChecker.isValue(seq)) {
		out.print("<script type='text/javascript'>alert('보드 파라미터가 없습니다.');history.back();</script>");
		return;
	}
	if (!PatternChecker.isNum(seq)) {
		out.print("<script type='text/javascript'>alert('보드 파라미터가 부정확합니다.');history.back();</script>");
		return;
	}

	int board_number = Integer.parseInt(seq);

	  
	BoardCategory bc = new BoardCategory();
	ArrayList<BoardCategoryItem> bc_list = bc.listBoardCategory();
	
	Member mem = new Member();
	ArrayList<MemberItem> mem_list = mem.listMember(0);
	

	Board board = new Board();
	BoardItem b_item = board.viewBoard(board_number);
	

	
	
	if (b_item == null) {
		out.print("<script type='text/javascript'>alert('데이터 조회에 실패했습니다. 잠시후 다시 시도해 주세요.')");
		return;
	}
	if (bc_list == null) {
		out.print("<script type='text/javascript'>alert('데이터 조회에 실패했습니다. 잠시후 다시 시도해 주세요.')");
		return;
	}
	if (mem_list == null) {
		out.print("<script type='text/javascript'>alert('데이터 조회에 실패했습니다. 잠시후 다시 시도해 주세요.')");
		return;
	}
	
%>
<!DOCTYPE html>
<html lang="ko">
  <head>
  <%@ include file="../head.jsp"%>
	
  </head>
  <body>
  	<%@ include file="../menu.jsp"%>
  
	<div class="container">
		<div class="page-header">
		<h1> 게시물 수정 - <small>게시물을 수정하세요.</small></h1>
	</div>
		
		<form id="frmBoardEdit" class="form-horizontal" role="form" method="post" action="editBoardOk.jsp">
		<input type="hidden" name="seq" value="<%=board_number%>">
			<fieldset>
				<div class="form-group">
					<label for="board_category_seq" class="col-md-2 control-label">게시판 카테고리</label>
					<div class="col-md-10">
						<select id="board_category_seq" name="board_category_seq" class="form-control">
							<option> ----- 선택하세요 ----- </option>
						<%
							if (bc_list != null) {
								for (int i=0; i<bc_list.size(); i++) {
									BoardCategoryItem item = bc_list.get(i);
									if (item.getSeq() == b_item.getBoard_category_seq()) {
						%>
							<option value="<%=item.getSeq()%>" selected><%=item.getCategory_name()%></option>
						<%				
									} else {
						%>
							<option value="<%=item.getSeq()%>"><%=item.getCategory_name()%></option>
						<%
									}
								}
							}
						%>
						</select>
					</div>
				</div>
				
				
			 
				<div class="form-group">
					<label for="member_seq" class="col-md-2 control-label">회원</label>
					<div class="col-md-10">
						<select id="member_seq" name="member_seq" class="form-control">
							<option> ----- 선택하세요 ----- </option>
						<%
							if (mem_list != null) {
								for (int i=0; i<mem_list.size(); i++) {
									MemberItem item = mem_list.get(i);
									if (item.getSeq() == b_item.getMember_seq()) {
						%>
							<option value="<%=item.getSeq()%>" selected><%=item.getUser_name()%> / <%=item.getEmail1()%>@<%=item.getEmail2()%></option>
						<%				
									} else {
						%>
							<option value="<%=item.getSeq()%>"><%=item.getUser_name()%> / <%=item.getEmail1()%>@<%=item.getEmail2()%></option>
						<%
									}
								}
							}
						%>
						</select>
					</div>
				</div>
							
				<!-- 입력양식 -->
				<div class="form-group">
					<label for="title" class="col-md-2 control-label">제목</label>
					<div class="col-md-10">
						<input type="text" class="form-control" id="title" placeholder="제목을 입력하세요" name="title" value=<%=b_item.getTitle()%> autofocus>
					</div>
				</div>
				
				<div class="form-group">
					<label for="content" class="col-md-2 control-label">내용</label>
					<div class="col-md-10">
						<textarea id="content" name="content" class="form-control" rows="10" placeholder="내용을 입력하세요"><%=b_item.getContent().replace("<br />", "\n")%></textarea>
					</div>
				</div>
					           
				<div class="form-group">
					<div class="col-md-offset-2 col-md-10">
						<div class="row">
							<div class="col-md-6">
								<button type="submit" class="btn btn-primary btn-block">수정하기</button>
							</div>
							<div class="col-md-6">
								<button type="reset" class="btn btn-warning btn-block">다시작성</button>
							</div>
						</div>
						
					</div>
				</div>
			</fieldset>
		</form>
	</div>
	<%DBHandler.disconnect();%>
	<!-- Javascript -->
	<script src="/italia/assets/js/jquery.js"></script>
	<script src="/italia/assets/js/bootstrap.min.js"></script>
	<!-- 형식검사 클래스 참조 -->
	<script src="/italia/assets/js/string.js"></script>
	<!-- 구현부분 -->
	<script type="text/javascript">
		// jquery main()
		$(function() {
			$("#frmBoardEdit").submit(function() {
				
				// 게시판 카테고리 선택여부 검사
				if ($("select[name='board_category_seq'] > option:selected").index() < 1) {
					alert("게시판 카테고리를 선택하세요.");
					$("select[name='board_category_seq']").focus();
					return false;
				}
				
				// 회원 선택여부 검사
				if ($("select[name='member_seq'] > option:selected").index() < 1) {
					alert("회원을 선택하세요.");
					$("select[name='member_seq']").focus();
					return false;
				}
				
				// 제목 입력여부 검사
				if (!$("input[name='title']").val().isValue()) {
					alert("제목을 입력하세요.");
					$("input[name='title']").focus();
					return false;
				}
				
				// 내용 입력여부 검사
				if (!$("textarea[name='content']").val().isValue()) {
					alert("내용을 입력하세요.");
					$("input[name='content']").focus();
					return false;
				}
			});
		});
	</script>
		
  </body>
</html>